package com.leetcode.partition15;

import java.util.*;

/**
 * @author `RKC`
 * @date 2021/8/6 9:31
 */
public class LC1418点菜展示表 {

    public static List<List<String>> displayTable(List<List<String>> orders) {
        List<List<String>> result = new ArrayList<>();
        Set<String> dishes = new TreeSet<>();
        Set<Integer> tableNumbers = new TreeSet<>();

        Map<String, Map<String, Integer>> map = new HashMap<>();
        for (List<String> order : orders) {
            String tableNumber = order.get(1);
            String dish = order.get(2);
            dishes.add(dish);
            tableNumbers.add(Integer.parseInt(tableNumber));
            if (!map.containsKey(tableNumber)) {
                map.put(tableNumber, new HashMap<>());
            }
            Map<String, Integer> dishMap = map.get(tableNumber);
            if (dishMap.containsKey(dish)) {
                //菜品已经点过，则进行自加操作
                dishMap.put(dish, dishMap.get(dish) + 1);
            } else {
                dishMap.put(dish, 1);
            }
        }
        List<String> tableHeadLine = new ArrayList<>(dishes.size() + 1);
        tableHeadLine.add("Table");
        tableHeadLine.addAll(dishes);
        result.add(tableHeadLine);

        for (Integer tableNumber : tableNumbers) {
            List<String> line = new ArrayList<>(dishes.size() + 1);
            String tns = String.valueOf(tableNumber);
            line.add(tns);
            for (String dish : dishes) {
                Map<String, Integer> dishMap = map.get(tns);
                //在map中查找是否存在
                if (dishMap.containsKey(dish)) {
                    //有点过，直接添加
                    line.add(String.valueOf(dishMap.get(dish)));
                    continue;
                }
                //没有点该菜品，直接添加为0
                line.add("0");
            }
            result.add(line);
        }
        return result;
    }

    public static void main(String[] args) {
        List<List<String>> orders = new ArrayList<>();
        List<String> l1 = new ArrayList<>(3);
        l1.add("David");
        l1.add("3");
        l1.add("Ceviche");
        List<String> l2 = new ArrayList<>(3);
        l2.add("Corina");
        l2.add("10");
        l2.add("Beef Burrito");
        orders.add(l1);
        orders.add(l2);
        System.out.println(displayTable(orders));
    }
}
